Calculating for leap year [migrated]
Posted
by
Bradley Bauer
on Programmers
See other posts from Programmers
or by Bradley Bauer
Published on 2012-10-12T04:03:36Z
Indexed on
2012/10/12
9:50 UTC
Read the original article
Hit count: 306
java
I've written this program using Java in Eclipse.
I was able to utilize a formula I found that I explained in the commented out section.
Using the for loop I can iterate through each month of the year, which I feel good about in that code, it seems clean and smooth to me. Maybe I could give the variables full names to make everything more readable but I'm just using the formula in its basic essence :)
Well my problem is it doesn't calculate correctly for years like 2008... Leap Years.
I know that
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
then we have a leap year.
Maybe if the year is a leap year I need to subtract a certain amount of days from a certain month.
Any solutions, or some direction would be great thanks :)
package exercises;
public class E28 {
/*
* Display the first days of each month
* Enter the year
* Enter first day of the year
*
* h = (q + (26 * (m + 1)) / 10 + k + k/4 + j/4 + 5j) % 7
*
* h is the day of the week (0: Saturday, 1: Sunday ......)
* q is the day of the month
* m is the month (3: March 4: April.... January and Feburary are 13 and 14)
* j is the century (year / 100)
* k is the year of the century (year %100)
*
*/
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter the year: ");
int year = input.nextInt();
int j = year / 100; // Find century for formula
int k = year % 100; // Find year of century for formula
// Loop iterates 12 times. Guess why.
for (int i = 1, m = i; i <= 12; i++) { // Make m = i. So loop processes formula once for each month
if (m == 1 || m == 2)
m += 12; // Formula requires that Jan and Feb are represented as 13 and 14
else
m = i; // if not jan or feb, then set m to i
int h = (1 + (26 * (m + 1)) / 10 + k + k/4 + j/4 + 5 * j) % 7; // Formula created by a really smart man somewhere
// I let the control variable i steer the direction of the formual's m value
String day;
if (h == 0)
day = "Saturday";
else if (h == 1)
day = "Sunday";
else if (h == 2)
day = "Monday";
else if (h == 3)
day = "Tuesday";
else if (h == 4)
day = "Wednesday";
else if (h == 5)
day = "Thursday";
else
day = "Friday";
switch (m) {
case 13:
System.out.println("January 1, " + year + " is " + day);
break;
case 14:
System.out.println("Feburary 1, " + year + " is " + day);
break;
case 3:
System.out.println("March 1, " + year + " is " + day);
break;
case 4:
System.out.println("April 1, " + year + " is " + day);
break;
case 5:
System.out.println("May 1, " + year + " is " + day);
break;
case 6:
System.out.println("June 1, " + year + " is " + day);
break;
case 7:
System.out.println("July 1, " + year + " is " + day);
break;
case 8:
System.out.println("August 1, " + year + " is " + day);
break;
case 9:
System.out.println("September 1, " + year + " is " + day);
break;
case 10:
System.out.println("October 1, " + year + " is " + day);
break;
case 11:
System.out.println("November 1, " + year + " is " + day);
break;
case 12:
System.out.println("December 1, " + year + " is " + day);
break;
}
}
}
}
© Programmers or respective owner